package com.hk.kindheartedness;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.hk.kindheartedness.db.DBOpenHelper;
import com.hk.kindheartedness.model.User;
import com.hk.kindheartedness.util.ToastUtil;

/**
 * 患者个人信息处理
 */
public class PatientPersonActivity extends AppCompatActivity implements View.OnClickListener {

    //    Initialize Toast
    private Toast toast;

    //    Initialize intent
    private Intent intent;

    //    声明DBOpenHelper对象
    private DBOpenHelper dbOpenHelper;

    private TextView loginPhone;

    private Button findMakeBtn,personInfoBtn,updatePwdBtn,logoutBtn;

    private String loginPhone2,loginUserId;

    @SuppressLint("LongLogTag")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patient_person);
//        实例化
        dbOpenHelper = new DBOpenHelper(PatientPersonActivity.this, "db_kindheartedness", null, 1);
//      提取出登录用户名
        Intent intent2 = getIntent();
        Bundle bundle = intent2.getExtras();
        loginPhone2 = bundle.getString("loginPhone");
        loginUserId = bundle.getString("loginUserId");
        loginPhone = findViewById(R.id.loginPhone);
        loginPhone.setText(loginPhone2);
//        获取按钮控制权
        findMakeBtn = findViewById(R.id.findMakeBtn);
        personInfoBtn = findViewById(R.id.personInfoBtn);
        updatePwdBtn = findViewById(R.id.updatePwdBtn);
        logoutBtn = findViewById(R.id.logoutBtn);
        findMakeBtn.setOnClickListener(this);
        personInfoBtn.setOnClickListener(this);
        updatePwdBtn.setOnClickListener(this);
        logoutBtn.setOnClickListener(this);

    }

    /**
     * 点击按钮事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            我的预约
            case R.id.findMakeBtn:
                intent = new Intent(PatientPersonActivity.this,PatientSubscribeInfoActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("loginUserId",loginUserId);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
//            查看个人资料
            case R.id.personInfoBtn:
//                回显
                findByPhonePerson();
                break;
//                修改密码
            case R.id.updatePwdBtn:
                findByPhone();
                break;
//                退出登录
            case R.id.logoutBtn:
                intent = new Intent(PatientPersonActivity.this,LoginActivity.class);
                startActivity(intent);
                PatientPersonActivity.this.finish();
                break;
            default:
                return;
        }
    }

    /**
     * 将登录手机号传递给下一个Activity
     */
    private void findByPhone() {
        String loginPhoneToString = loginPhone.getText().toString();
        intent = new Intent(PatientPersonActivity.this,PatientUpdatePwdActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("loginPhone",loginPhoneToString);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     * 通过登录手机号查询账号个人信息
     */
    private void findByPhonePerson() {
        String loginPhoneToString = loginPhone.getText().toString();
        SQLiteDatabase readableDatabase = dbOpenHelper.getReadableDatabase();
        Cursor queryUser = readableDatabase.query("user", null, "phone = ?", new String[]{loginPhoneToString}, null, null, null);
        User user = null;
        while (queryUser.moveToNext()) {
            String id = queryUser.getString(0);
            String username = queryUser.getString(1);
            String phone = queryUser.getString(2);
            String medicalCardNumber = queryUser.getString(5);
            String sex = queryUser.getString(6);
            String age = queryUser.getString(7);
            String idNumber = queryUser.getString(8);
            String address = queryUser.getString(9);
//            通过构造函数进行封装
            user = new User(Integer.parseInt(id),username,phone,medicalCardNumber,Integer.parseInt(sex),Integer.parseInt(age),idNumber,address);
        }
        if (user == null) {
            toast = ToastUtil.toastHint(toast,PatientPersonActivity.this,"未查到当前登录账号个人信息,请稍后重试！");
            return;
        }
        intent = new Intent(PatientPersonActivity.this,PatientPersonInfoActivity.class);
        String toJSONString = JSON.toJSONString(user);
        Bundle bundle = new Bundle();
        bundle.putString("user",toJSONString);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     * 断开数据库连接
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dbOpenHelper != null) {
            dbOpenHelper.close();
        }
    }

}